package com.hsy.baoinbao.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.Window;
import android.widget.Toast;

import com.hsy.baoinbao.common.Config;
import com.hsy.baoinbao.common.Consts;

/**
 * Created by hsy on 2016/3/23.
 */
public class CommonUtils {
    public static String INVALID_TOKEN = "-1";

    public static final String INVALID_UID = "INVALID_UID";
    private static String mUid = INVALID_UID;

    public static void saveToken(Context context, String token) {
        CommonUtils.setUid(context, token);
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putString(Consts.KEY_TOKEN, token).commit();
    }

    public static String getToken(Context context) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getString(Consts.KEY_TOKEN, INVALID_TOKEN);
    }

    public static void clearToken(Context context) {
        CommonUtils.clearUid(context);
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().remove(Consts.KEY_TOKEN).commit();
    }

    private static Toast toast;

    public static void showToast(Context applicationContext, String msg) {
        if (null == toast) {
            toast = Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public static void showToast(Context applicationContext, int msg) {
        CommonUtils.showToast(applicationContext, applicationContext.getString(msg));
    }

    public static boolean checkLogin(Context context) {
        String token = getToken(context);
        if (INVALID_TOKEN.equals(token)) {
            return false;
        }
        return true;
    }

    private static ProgressDialog mProgressDialog = null;

    public static void showProgressDialog(Context context) {
        hideProgressDialog();
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mProgressDialog.setMessage("数据加载中，请稍后...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    public static void hideProgressDialog() {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public static void setUid(Context context, String mUid) {
        CommonUtils.mUid = mUid;
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putString(Consts.UID, mUid).commit();
    }

    public static String getUid(Context context) {
        if (INVALID_UID.equals(CommonUtils.mUid)) {
            CommonUtils.mUid = context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getString(Consts.UID, INVALID_UID);
        }
        return CommonUtils.mUid;
    }

    public static void clearUid(Context context) {
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().remove(Consts.UID).commit();
    }
}
